XML DOM SUMMARY!
=================
DOM Introduction
-----------------
The XML DOM (Document Object Model) represents an XML document as a structured tree. Each element, attribute, and piece of text is a node. DOM provides a way to dynamically access and modify the content and structure of XML documents using programming languages like JavaScript, Java, and Python.
DOM Nodes
---------
In the DOM tree, every part of an XML document is a node:
- Document node
- Element node
- Attribute node
- Text node
- Comment node
Each type of node has properties and methods specific to its role.
DOM Accessing
-------------
Nodes are accessed through methods like:
- `getElementsByTagName()`
- `getElementById()`
- `getElementsByClassName()`
Accessing nodes is essential for reading and modifying XML documents.
Example:
.. code-block:: xml
Harry Potter
Access title element:
.. code-block:: html
DOM Node Info
-------------
Each node provides information through properties:
- `nodeName`: the name of the node
- `nodeValue`: the value of the node (if text/attribute)
- `nodeType`: the type (1 for Element, 2 for Attribute, 3 for Text)
Example:
.. code-block:: html
DOM Node List
-------------
A NodeList is a collection of nodes, usually returned by methods like `getElementsByTagName()`. It behaves like an array but not exactly — it can be live (auto-updating) or static.
Example:
.. code-block:: html
DOM Traversing
--------------
Traversing refers to moving across the DOM tree:
- `parentNode`
- `childNodes`
- `firstChild`
- `lastChild`
- `nextSibling`
- `previousSibling`
Example:
.. code-block:: html
DOM Navigating
--------------
DOM navigation properties allow movement between related nodes:
- Move to parent, child, or sibling nodes.
- Useful for dynamically working with any XML structure.
Example:
.. code-block:: html
DOM Get Values
--------------
Retrieving values is a frequent operation:
- Element text: via `nodeValue`
- Attribute values: via `getAttribute()`
Example:
.. code-block:: html
DOM Change Nodes
----------------
You can change node values:
- Update text content
- Update attributes
Example:
.. code-block:: html
DOM Remove Nodes
----------------
Nodes can be removed using `removeChild()`.
Example:
.. code-block:: html
DOM Replace Nodes
-----------------
Nodes can be replaced using `replaceChild(newNode, oldNode)`.
Example:
.. code-block:: html
DOM Create Nodes
----------------
New nodes are created using:
- `createElement()`
- `createTextNode()`
Example:
.. code-block:: html
DOM Add Nodes
-------------
Adding new nodes involves creating a node and appending it to an existing parent.
Example:
.. code-block:: html
DOM Clone Nodes
---------------
Nodes can be duplicated using `cloneNode(true/false)`:
- `true` means deep clone (with all children)
- `false` means shallow clone (without children)
Example:
.. code-block:: html
DOM Examples
------------
Examples combine everything:
- Load an XML document
- Access and modify elements
- Create new nodes dynamically
Full Example:
.. code-block:: html